<?php
namespace Admin\Controller;
class UserController extends AdminController {
	public function index() {
		A("Worker")->sendMsg("http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=你好美丽啊了十块剪短发哈了十块法拉盛就拉丝机符老师拉斯柯达飞");
		$this -> display();
	}

	public function view() {
		//Load('extend');
		$n = D("User");
		import("ORG.Util.Page");
		//导入分页类
		$page = empty($_POST["page"])?1:$_POST["page"];
		$rows = empty(I("limit"))?1:I("limit");
		$sort = empty($_POST["sort"])?'id':$_POST["sort"];
		$order = empty($_POST["order"])?'desc':$_POST["order"];
		//dump($order);
		if ($_POST["account"] != null && $_POST["account"] != "") {
			$condition['account'] = array('like', '%' . $_POST["account"] . '%');
		}
		if ($_POST["nickname"] != null && $_POST["nickname"] != "") {
			$condition['nickname'] = array('like', '%' . $_POST["nickname"] . '%');
		}
		if ($_POST["createdatetimeStart"] != null && $_POST["createdatetimeStart"] != "") {
			$condition['create_time'] = array('between', $_POST["createdatetimeStart"], $_POST["createdatetimeEnd"]);
		}
		if ($_POST["modifydatetimeStart"] != null && $_POST["modifydatetimeStart"] != "") {
			$condition['update_time'] = array('between', $_POST["modifydatetimeStart"], $_POST["modifydatetimeEnd"]);
		}
		$count = $n -> where($condition) -> count();
		//计算总数
		//$p = new Page($count,$rows);order($sort+','+$order)->

		$list = $n -> relation(true) -> where($condition) -> page($page, $rows) -> order(array($sort => $order)) -> select();
		//dump($list);
		$list = json_encode($list);
		$result = '{"status":1,"msg":"成功啦","total":' . $count . ',"rows":' . $list . '}';
		echo($result);
	}

	//添加用户提交处理
	public function add() {
		if (empty($_POST)) {
			$workid = I('workid');
			$this -> assign('workid', $workid);
			
			$rolelist=M('Role')->order('id asc')->select();
			$this -> assign('rolelist', $rolelist);
			$this -> display();
		} else {
			$m = D("User");
			$_POST['password'] = md5(123456);
			$roleId = $_POST['roleid'];
			if (!$vo = $m -> create()) {
				$this -> error($m -> getError(), 3);
			} else {
				if ($result = $m -> add($vo)) {
					//dump($result);
					$this -> addRole($result, $roleId);
					$this -> success('添加用户成功！', 1);
				} else {
					$this -> error('添加用户失败！', 0);
				}
			}
		}
	}

	protected function addRole($userId, $roleId) {
		$RoleUser = M("role_user");
		$data['uid'] = $userId;
		$data['group_id'] = $roleId;
		$RoleUser -> add($data);
	}

	protected function addGroup($userId, $groupsIds) {
		$RoleUser = M("groups_user");
		foreach ($groupsIds as $key => $v) {
			//$userlist[$key]=$v[id];
			$data['user_id'] = $userId;
			$data['groups_id'] = $v;
			$RoleUser -> create($data);
			$RoleUser -> add($data);
		}

	}

	// 更新数据
	public function edit() {
		if (empty($_POST)) {
			$workid = I('workid');
			$this -> assign('workid', $workid);
			
			$rolelist=M('Role')->order('id asc')->select();
			$this -> assign('rolelist', $rolelist);
			$this -> display();
		} else {
			$Form = D("User");
			$userId = $_POST['id'];
			$roleId = $_POST['roleid'];
			$vo = $Form -> create();
			if ($vo!=false) {
				$list = $Form -> save($vo);
				$this -> editRole($userId, $roleId);
				$this -> success('更新成功！', 1);
			} else {
				$this -> error($Form -> getError(), 0);
			}
		}
	}

	protected function editRole($userId, $roleId) {
		$RoleUser = M("role_user");
		//dump($roleId);
		$data['uid'] = $userId;
		$data['group_id'] = $roleId;
		$con['uid'] = $userId;
		$RoleUser -> where($con) -> delete();
		$res=$RoleUser -> add($data);
	}

	protected function editGroup($userId, $groupsIds) {
		$GroupUser = M("groups_user");
		$condition['user_id'] = $userId;
		$GroupUser -> where($condition) -> delete();
		foreach ($groupsIds as $key => $v) {
			//$userlist[$key]=$v[id];
			$data['user_id'] = $userId;
			$data['groups_id'] = $v;
			$GroupUser -> create($data);
			$GroupUser -> add($data);
		}
	}

	public function delete() {
		if (!empty($_GET['ids'])) {
			$d = M("user");
			$condition['id'] = array('in', $_GET['ids']);
			$result = $d -> where($condition) -> delete();
			if (false !== $result) {
				$RoleUser = M("role_user");
				$condition1['uid'] = array('in', $_GET['ids']);
				$RoleUser -> where($condition1) -> delete();
				$this -> success('删除用户成功！', 1);
			} else {
				$this -> error('删除出错！', 0);
			}
		} else {
			$this -> error('删除项不存在！', 0);
		}
		//$this->redirect('index');
	}

	public function editPwd() {
		if (empty($_POST)) {
			$this -> display();
		} else {
			$Form = D("user");
			$_POST['password'] = md5($_POST['password']);
			$list = $Form -> save($_POST);
			if ($list !== false) {
				$this -> success('更新成功！', 1);
			} else {
				$this -> error('没有更新任何数据!', 0);
			}
		}
	}

	//用户公共信息组件
	public function viewWidget() {
		$n = D("User");
		$list = $n -> select();
		$list = json_encode($list);
		echo($list);
		//输出json数据
	}

}
?>